package com.blue.Impl.workflow;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.blue.common.constant.CommonConstant;
import com.blue.common.utils.ConvertUtils;
import com.blue.vo.workflow.BusinessFieldVO;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.blue.mapper.workflow.BusinessFieldMapper;
import com.blue.po.workflow.BusinessFieldPO;
import com.blue.service.workflow.IBusinessFieldService;

import java.util.ArrayList;
import java.util.List;

import static java.util.Arrays.stream;


@Service
public class BusinessFieldServiceImpl extends ServiceImpl<BusinessFieldMapper, BusinessFieldPO> implements IBusinessFieldService {

    @Override
    public List<BusinessFieldVO> getFields(String definitionId) {
        List<BusinessFieldPO> businessFieldPOS = this.list(new LambdaQueryWrapper<BusinessFieldPO>()
                .eq(BusinessFieldPO::getDefinitionId, definitionId)
                .orderByAsc(BusinessFieldPO::getSort));
        List<BusinessFieldVO> businessFieldVOS = ConvertUtils.convertList(BusinessFieldVO.class, businessFieldPOS);
        List<BusinessFieldVO> businessFields = businessFieldVOS.stream()
                .filter(item -> CommonConstant.NO.equals(item.getTableFlag()))
                .toList();
        businessFields.forEach(businessField -> {
            List<BusinessFieldVO> tableFields = new ArrayList<>();
            businessFieldVOS.forEach(item -> {
                if (CommonConstant.YES.equals(item.getTableFlag()) && businessField.getId().equals(item.getFieldId())) {
                    tableFields.add(item);
                }
            });
            businessField.setTableFields(tableFields);
        });
        return businessFields;
    }
}
